package net.angrynut.web.model.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import net.angrynut.web.model.entity.User;

public class UserValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		if (target == null)
			errors.reject("ERR_TARGET_IS_NULL");

		User user = (User) target;
		if (user.getUsername() == null || user.getUsername().length() < 3) {
			errors.rejectValue("username", "ERR_USERNAME", "用户名不正确");
		} else if (user.getPassword() == null) {
			errors.rejectValue("password", "ERR_PASSWORD", "密码不正确");
		}
	}

}
